package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func MiddleWare() gin.HandlerFunc {
	return func(c *gin.Context) {

		t := time.Now()
		fmt.Println("中间件开始执行...")
		c.Set("Request", "中间件")
		//Next转回到控制器
		c.Next()
		status := c.Writer.Status()
		fmt.Println("中间件执行完毕", status)
		t2 := time.Since(t)
		fmt.Println("time:", t2)
	}
}

func main() {
	r := gin.Default()
	r.Use(MiddleWare())
	{
		r.GET("/ce", func(c *gin.Context) {
			req, _ := c.Get("Request")
			fmt.Println("Request:", req)
			c.JSON(http.StatusOK, gin.H{
				"request": req,
			})
		})
	}
	r.Run(":8888")
}
